package com.qf.shopping.service.impl;

import com.qf.shopping.dao.*;
import com.qf.shopping.dao.impl.*;
import com.qf.shopping.entity.Address;
import com.qf.shopping.entity.Order;
import com.qf.shopping.entity.OrderDetail;
import com.qf.shopping.entity.ShoppingCart;
import com.qf.shopping.service.IOrderService;
import com.qf.shopping.utils.Tools;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 三旬
 * @version 1.0  2022/9/23
 */

public class OrderServiceImpl extends BaseServiceImpl<Order> implements IOrderService {
    //创建访问dao层的对象
    IOrderDao orderDao = new OrderDaoImpl();
    IAddressDao addressDao = new AddressDaoImpl();
    ICartDao cartDao = new CartDaoImpl();
    IOrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
    IProductDao productDao = new ProductDaoImpl();

    @Override
    protected BaseDao<Order> getBaseDao() {
        return orderDao;
    }

    /**
     * 生成订单处理
     * @param uid
     * @param addressId
     * @param bank
     * @param express
     * @return
     */
    @Override
    public Integer createOrder(Integer uid, Integer addressId, String bank, String express) {
        //生成订单
        try {
            //关闭自动 提交事务，开启手动提交
            Tools.begin();
            /*1、获得订单的数据、封装、生成订单*/
            //获得地址信息
            Address address = addressDao.selectOneById(addressId);
            //获取购物车信息，计算购买商品的总金额
            List<ShoppingCart> carts = cartDao.selectProductByUid(uid);

            BigDecimal totalPrice = new BigDecimal(0.0);
            for (ShoppingCart cart : carts) {
                //根据购物车的商品pid,获得商品的详细信息
                cart.setProduct(productDao.selectOneById(cart.getPid()));
                totalPrice = totalPrice.add(cart.getProduct().getPrice().multiply(BigDecimal.valueOf(cart.getNums())));
            }
            //封装订单数据
            Order order = new Order();
            order.setStatu(0);
            order.setTotalPrice(totalPrice);
            order.setUid(uid);
            order.setPayType(bank);
            order.setSendType(express);
            order.setToName(address.getToName());
            order.setPhone(address.getPhone());
            order.setAddress(address.getAddress());
            //生成订单信息，返回生成的订单号(主键回填：数据插入成功后返回主键)
            Integer oid = orderDao.insert(order);
            //2、获得订单详情的数据、封装、生成订单详情数据
            //根据购物车的商品类型数量生成对应的订单详情数据（一对多关系1：n）
            for (ShoppingCart cart : carts) {
                OrderDetail o = new OrderDetail();
                o.setOid(oid);
                o.setPid(cart.getPid());
                o.setPname(cart.getProduct().getName());
                o.setDes(cart.getProduct().getDes());
                o.setPngUrl(cart.getProduct().getPngUrl());
                o.setPrice(cart.getProduct().getPrice());
                o.setNums(cart.getNums());
                o.setTotal(cart.getProduct().getPrice().multiply(BigDecimal.valueOf(cart.getNums())));
                //插入数据,生成订单详情数据
                orderDetailDao.insert(o);
            }

            //3、清空当前用户购物车信息
            //当前设计为一次全部下单，一次全部清除
            cartDao.deleteById(uid);
            //提交事务
            Tools.commit();
            //4、不做验证，直接返回订单编号
            return oid;
        }catch (Exception e){
            Tools.rollback();
        }
        return 0;
    }
}
